<?php

namespace app\api\controller;

use app\api\validate\Partner;
use app\common\controller\Api;
use app\common\model\FirmConfig;
use app\common\model\FirmContact;
use app\common\model\FirmHome;
use app\common\model\FirmIntroducer;
use app\common\model\FirmLabel;
use app\common\model\FirmMilestone;
use app\common\model\FirmPartner;
use app\common\model\FirmServe;
use app\common\model\FirmUrl;
use app\common\model\NewsCategory;
use app\common\model\NewsHome;
use app\common\model\PartnerCase;
use app\common\model\PartnerHome;
use app\common\model\ServeHome;
use app\common\model\Store;
use app\common\model\StoreCate;
use app\common\model\News as NewsModel;
use app\common\model\TeamMember;

/**
 * 首页接口
 */
class Index extends Api
{
    /**
     * 产品服务
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_product_list()
    {
        $home = ServeHome::order('id','DESC')->find();

        if($home){
            $home['img'] = getRealImg($home['img']);
            $home['mobile_image'] = getRealImg($home['mobile_image']);
        }
        $data['home'] = $home;
        $data['product'] = FirmServe::where('status',FirmServe::STATUS_TWO)->field('content,en_content,mobile_content,mobile_en_content，serve_img,status,weigh,create_time,mobile_serve_img',true)->order('weigh')->select()->each(function ($item){
            $item["icon"] = getRealImg($item['icon']);
            $item["checked_icon"] = getRealImg($item['checked_icon']);
            $item['path'] = FirmUrl::where('id',$item['path'])->value('path');
            if($item['banner']){
                $banner = explode(',',$item['banner']);
                foreach ($banner as &$value){
                    $value = getRealImg($value);
                }
                $item['banner'] = $banner;
            }else{
                $item['banner'] = [];
            }
        });
        $this->success($data);
    }

    /**
     * 联系我们
     */
    public function contact()
    {
        $home = FirmContact::order('id','DESC')->find();
        if($home){
            $home['contact_img'] = getRealImg($home['contact_img']);
        }
        $config = FirmConfig::field('mobile,email,address,web_name,en_address,en_web_name,tx_map_key,lat,lng')->order('id','DESC')->find();
        $this->success(compact('home','config'));
    }

    /**
     * 资讯详情
     */
    public function news_info()
    {
        $ids = $this->request->param('ids/d',0);
        if(!$ids) $this->error('缺少关键字ID');
        $news = NewsModel::where('id',$ids)->where('is_release',NewsModel::RELEASE_YAT)->field('create_time,is_release,weigh,browse,info_image,web_title,web_keywords,web_description,en_introduction,en_web_title,en_web_keywords,en_web_description,introduction',true)->find();
        if($news){
            $news['image'] = getRealImg($news['image']);
            $news["content"] = replace_editor_content_images_url($news["content"]);
            $news["en_content"] = replace_editor_content_images_url($news["en_content"]);
        }else{
            $news = [];
        }

        $home = NewsHome::order('id','DESC')->find();
        $home['news_img'] = getRealImg($home['news_img']);
        $this->success(compact('home','news'));
    }

    /**
     * 更多资讯
     */
    public function more_news()
    {
        $ids = $this->request->param('ids/d',0);
        if(!$ids) $this->error('缺少关键字ID');
        $category = NewsCategory::where('id',$ids)->field('id,name,en_name')->find();
        if(!$category) $this->error('该类型不存在');
        $page = $this->request->param('page/d',1);
        $limit = $this->request->param('limit/d',10);

        $news = NewsModel::where('type',$ids)->where('is_release',NewsModel::RELEASE_YAT)->order('update_time','DESC')->order('weigh')->field('content,en_content,create_time,is_release,weigh,browse,info_image,web_title,web_keywords,web_description,en_introduction,en_web_title,en_web_keywords,en_web_description,introduction',true);
        if($page){
            $news = $news->paginate($limit);
        }else{
            $news = $news->select();
        }
        foreach ($news as $item){
            $item['image'] = getRealImg($item['image']);
        }

        $home = NewsHome::order('id','DESC')->find();
        $home['news_img'] = getRealImg($home['news_img']);

        $this->success(compact('home','news'));
    }

    /**
     * 资讯动态首页数据
     */
    public function message_home()
    {
        $home = NewsHome::order('id','DESC')->find();
        $home['news_img'] = getRealImg($home['news_img']);
        $category = NewsCategory::where('status',NewsCategory::STATUS_TWO)->field('id,name,en_name')->select();
        foreach ($category as $item){
            $item['date'] = NewsModel::where('type',$item['id'])->where('is_release',NewsModel::RELEASE_YAT)->order('update_time','DESC')->order('weigh')->field('content,en_content,create_time,is_release,weigh,browse,info_image,web_title,web_keywords,web_description,en_introduction,en_web_title,en_web_keywords,en_web_description,introduction',true)->limit(4)->select()->each(function ($item){
                $item['image'] = getRealImg($item['image']);
            });
        }
       $this->success(compact('home','category'));
    }


    /**
     * 经典案例
     */
    public function classic()
    {
        $ids = $this->request->get('ids/d',1);
        $limit = $this->request->get('limit/d',8);
        $page = ($this->request->get('page/d',1) -1 ) * $limit;

        $where['cate_id'] = $ids;
        $where['status'] = PartnerCase::STATUS_TWO;
        $classic = PartnerCase::where($where)->order('weigh')->field('id,name,en_name,case_img,case_introduce,en_case_introduce,color')->page($page)->paginate($limit)->each(function ($item){
            $item['case_img'] = getRealImg($item['case_img']);
        });
        $this->success($classic);
    }

    /**
     * 合作客户
     */
    public function partner_client()
    {
        $array = [];
        $home = PartnerHome::order('id','DESC')->find();
        if($home){
            $home['partner_img'] = getRealImg($home['partner_img']);
        }
        $array['cate'] = StoreCate::where('status',StoreCate::NORMAL)->order('weigh')->field('id,name,en_name')->select();
        $array['client'] = Store::where('is_show',Store::IS_SHOW_YES)->field('name,en_name,image')->select()->each(function ($item){
            $item['image'] = getRealImg($item['image']);
        });
        $array['home'] = $home;
        return $this->success($array);
    }

    /**
     *企业里程碑
     */
    public function firm_milestone()
    {
        $limit = $this->request->param('limit/d',7);
        $milestone_log = FirmMilestone::where('status',FirmMilestone::STATUS_TWO)->order('year')->field('status,create_time',true)->paginate($limit);
        return $this->success($milestone_log);
    }


    /**
     * 企业介绍
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException0
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function firm_introduce()
    {
        $info = FirmIntroducer::order('id','DESC')->find();
        if($info){
            $banner = $info['team_banner'] ? explode(',',$info['team_banner']) : [];
            $team_member = $info['team_member'] ? explode(',',$info['team_member']) : []; //团队成员个人照片
            if($banner){
                foreach ($banner as &$item){
                    $item = getRealImg($item);
                }
            }
            if($team_member){
                foreach ($team_member as &$item){
                    $item = getRealImg($item);
                }
            }
            $info['team_banner'] = $banner;
            $info['team_member'] = $team_member;
            $info['team'] = getRealImg($info['team']);
            $info['video'] = getRealImg($info['video']);
            $info['voice'] = getRealImg($info['voice']);
            $info['with_img'] = getRealImg($info['with_img']);
            $info['firm_img'] = getRealImg($info['firm_img']);
        }else{
            $info = [];
        }
        return $this->success($info);
    }

    /**
     * 团队成员个人照片
     */
    public function team_member_photos(){
        $data = TeamMember::where('is_show', TeamMember::IS_SHOW_YES)->where('type', TeamMember::TYPE_MEMBER_ONE)->field('id,name,image,job,des')->order('weigh decs')->select();
        foreach($data as &$item){
            $item['image'] = getRealImg($item['image']);
        }
        return $this->success("OK", $data);
    }

    //  首页公司基本信息
    public function get_site_info()
    {
        $info = FirmConfig::order('id','DESC')->find();
        if($info){
            $info['logo'] = getRealImg($info['logo']);
            $info['web_code'] = getRealImg($info['web_code']);
        }else{
            $info = [];
        }
        $label = FirmLabel::where('status',FirmLabel::STATUS_TWO)->field('label_name,en_label_name,url_id,label_type,icon')->order('weigh')->select()->each(function ($item){
            $item['url'] = FirmUrl::where('id',$item['url_id'])->value('path');
        })->toArray();

        foreach($label as &$item){
            $item['icon'] = getRealImg($item['icon']);
        }
        $array['top'] = array_values(array_filter($label, function ($val) {
            return $val['label_type'] == FirmLabel::TOP;
        }));
        $array['bottom'] = array_values(array_filter($label, function ($val) {
            return $val['label_type'] != FirmLabel::TOP;
        }));
        $array['info'] = $info;
        return $this->success("获取成功",$array);
    }

    /**
     * 我们的服务(分类)
     */
    public function get_server()
    {
        $home = FirmHome::order('id','DESC')->find();
        $data = FirmServe::where('status',FirmServe::STATUS_TWO)->field('status,weigh,create_time',true)->order('weigh')->select()->each(function ($item){
            $item["icon"] = getRealImg($item['icon']);
            $item["mobile_serve_img"] = getRealImg($item['mobile_serve_img']);
            $item["serve_img"] = getRealImg($item['serve_img']);
            $item["path"] = FirmUrl::where('id',$item['path'])->value('path');
        })->toArray();
        if($home){
            $home['video'] = getRealImg($home['video']);
            $home['voice'] = getRealImg($home['voice']);
            $home['mobile_image'] = getRealImg($home['mobile_image']);
            array_unshift($data,$home);
        }
        return $this->success("获取成功",$data);
    }

    /**
     * 合作需求
     */
    public function partner()
    {
        if (!$this->request->isPost()) {
            $this->error('请求方式错误！');
        }
        $data = $this->request->post();
        $validate = new Partner();

        if (!$validate->check($data, [], 'add')) {
            $this->error($validate->getError());
        }

        $res = FirmPartner::create($data);
        if($res){
            $this->success('您的信息已提交！');
        }else{
            $this->error('提交失败!');
        }
    }




    /**
     * 头部
     * logo menu
     */
    public function get_site_header()
    {
        $info = model('Site')->field("logo,name,en_name")->find();
        $info['logo'] = getRealImg($info['logo']);
        return $this->success("",$info);
    }
    /**
     *
     */
    public  function get_site_center(){

    }
    /**
     * 底部
     */
    public function get_site_foot()
    {
        $info = model('Site')->field("wechat,wechat_code,qq,email,mobile,icp,name,address,logo,en_name,en_address")->find();
        $info['logo'] = getRealImg($info['logo']);
        $wechat_code = $info['wechat_code'] ? explode(",",$info['wechat_code']) : [];
        foreach($wechat_code as &$item){
            $item = getRealImg($item);
        }
        $info['wechat_code'] = $wechat_code;
        $index_image = $info['index_image'] ? explode(",",$info['index_image']) : [];
        foreach($index_image as &$item){
            $item = getRealImg($item);
        }
        $info['index_image'] = $index_image;

        $en_index_image = $info['en_index_image'] ? explode(",",$info['en_index_image']) : [];
        foreach($en_index_image as &$item){
            $item = getRealImg($item);
        }
        $info['en_index_image'] = $en_index_image;
        return $this->success("",$info);
    }



    /**
     * 首页轮播
     */
    public function getBanner(){
        $data = model('InformationBanner')->where('status',"normal")->order('weigh desc')->select();
        foreach($data as $item){
            $item['image'] = getRealImg($item['image']);
        }
        return $this->success("获取成功",$data);
    }

}
